<?php
    class Game extends CActiveRecord
    {
        public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }

        public function tableName() {                  
            return 'c_game';
        }
        
        
        public function getGameOnline($currentPage,$numberRecordPerPage)
        {
            $startRecord = ($currentPage - 1) * $numberRecordPerPage;
            $sql = "SELECT id, title,introtext, alias,picture,percent_wapmaster,download,create_date,isJava,isAndroid,isIos,isRim FROM c_game WHERE  isOnline= 1 AND status =1 ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();   
            return $rows;
        }
        public function getGameOnlineHot($currentPage,$numberRecordPerPage)
        {
            $startRecord = ($currentPage - 1) * $numberRecordPerPage;
            $sql = "SELECT id, title,introtext, alias,picture,percent_wapmaster,download,create_date,isJava,isAndroid,isIos,isRim FROM c_game WHERE  isOnline= 1 AND status =1 ORDER BY download DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();   
            return $rows;
        }
        
        public function getGameOffline($currentPage,$numberRecordPerPage)
        {
            $startRecord = ($currentPage - 1) * $numberRecordPerPage;
            $sql = "SELECT id, title,introtext, alias,picture,download,percent_wapmaster,create_date,isJava,isAndroid,isIos,isRim FROM c_game WHERE  isOnline= 0 AND status=1 ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();   
            return $rows;
        }
        public function getGameOfflineHot($currentPage,$numberRecordPerPage)
        {
            $startRecord = ($currentPage - 1) * $numberRecordPerPage;
            $sql = "SELECT id, title,introtext, alias,picture,download,percent_wapmaster,create_date,isJava,isAndroid,isIos,isRim FROM c_game WHERE  isOnline= 0 AND status=1 ORDER BY download DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();   
            return $rows;
        }
        
        public function getTotalGameOnline()
        {
            $sql = "SELECT count(id) FROM c_game WHERE  isOnline= 1 AND status=1 ";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }
        
        public function getTotalGameOffline()
        {
            $sql = "SELECT count(id) FROM c_game WHERE  isOnline= 0 AND status=1 ";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }
        
        /* Thông tin về game */
        public function getInfoGame($id)
        {
            $sql = "SELECT * FROM c_game WHERE id=" . $id;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();   
            return $rows;   
        }
        
        public function getDataLastest($page,$num_per_page,$router='user/index',$type_online=1){
        $cacheService = new CacheService("Game","getDataLastest",$page.$num_per_page.$router);
        $key = $cacheService->createKey();
        $cache = Yii::app()->cache->get($key);
        $cache=false;
        if($cache==false){
            $connect =Yii::app()->db;
            $sub_sql='status=1 AND isOnline='.$type_online;
            $sql="SELECT count(id) as total FROM c_game WHERE ".$sub_sql."";
            $command=$connect->createCommand($sql);
            $row = $command->queryRow();
            $total=$row['total'];
            $begin = ($page - 1) * $num_per_page;
            $sql="SELECT id,categoryId,title,alias,introtext,picture,download,hit,create_date,isHot,percent_wapmaster,isOnline,isSms 
            FROM c_game WHERE ".$sub_sql." ORDER BY create_date DESC LIMIT ".$begin.",".$num_per_page."";
            
           
           
            $command=$connect->createCommand($sql);
            $rows = $command->queryAll();            

            $num_page=ceil($total/$num_per_page);
            $url=Url::createUrl($router,array('type_cat'=>$type_online,'type_data'=>1));
            $url1=$url;
            $url.='/';
            $paging=Paging::show_paging_wap_user($num_page,$page,$url,$url1);
            $a=array($rows,$paging);
            // var_dump($a);die;
            Yii::app()->cache->set($key, $a, ConstantsUtil::TIME_CACHE_900);
        }else{
            $a = $cache;
        }
        return $a;
    }  
     
    public function getDataManySms($page,$num_per_page,$router='user/index',$type_online=2){
        $cacheService = new CacheService("Game","getDataManySms",$page.$num_per_page.$router);
        $key = $cacheService->createKey();
        $cache = Yii::app()->cache->get($key);
        $cache=false;
        if($cache==false){
            $connect =Yii::app()->db;
            if($type_online >= 2){   
                $sub_sql='status=1 AND manySms =1';
            }else{
                $sub_sql='status=1 AND manySms =1 AND isOnline='.$type_online;
            }
            $sql="SELECT count(id) as total FROM c_game WHERE ".$sub_sql."";
            $command=$connect->createCommand($sql);
            $row = $command->queryRow();
            $total=$row['total'];
            $begin = ($page - 1) * $num_per_page;
            $sql="SELECT id,categoryId,title,alias,introtext,picture,download,hit,create_date,isHot,percent_wapmaster,isOnline,isSms 
            FROM c_game WHERE ".$sub_sql." ORDER BY create_date DESC LIMIT ".$begin.",".$num_per_page."";
            //echo $sql; die;
            $command=$connect->createCommand($sql);
            $rows = $command->queryAll();            

            $num_page=ceil($total/$num_per_page);
            $url=Url::createUrl($router,array('type_cat'=>$type_online,'type_data'=>2));
            $url1=$url;
            $url.='/';
            $paging=Paging::show_paging_wap_user($num_page,$page,$url,$url1);
            $a=array($rows,$paging);
            
            Yii::app()->cache->set($key, $a, ConstantsUtil::TIME_CACHE_900);
        }else{
            $a = $cache;
        }
        return $a;
    }
    public function getDataManyCard($page,$num_per_page,$router='user/index',$type_online=2){
        $cacheService = new CacheService("Game","getDataManyCard",$page.$num_per_page.$router);
        $key = $cacheService->createKey();
        $cache = Yii::app()->cache->get($key);
        $cache=false;
        if($cache==false){
            $connect =Yii::app()->db;
            if($type_online >= 2){   
                $sub_sql='status=1 AND manyCard =1';
            }else{
                $sub_sql='status=1 AND manyCard =1 AND isOnline='.$type_online;
            }
            $sql="SELECT count(id) as total FROM c_game WHERE ".$sub_sql."";
            $command=$connect->createCommand($sql);
            $row = $command->queryRow();
            $total=$row['total'];
            $begin = ($page - 1) * $num_per_page;
            $sql="SELECT id,categoryId,title,alias,introtext,picture,download,hit,create_date,isHot,percent_wapmaster,isOnline,isSms 
            FROM c_game WHERE ".$sub_sql." ORDER BY create_date DESC LIMIT ".$begin.",".$num_per_page."";
            $command=$connect->createCommand($sql);
            $rows = $command->queryAll();            

            $num_page=ceil($total/$num_per_page);
            $url=Url::createUrl($router,array('type_cat'=>$type_online,'type_data'=>4));
            $url1=$url;
            $url.='/';
            $paging=Paging::show_paging_wap_user($num_page,$page,$url,$url1);
            $a=array($rows,$paging);
            
            Yii::app()->cache->set($key, $a, ConstantsUtil::TIME_CACHE_900);
        }else{
            $a = $cache;
        }
        return $a;
    }
    public function getDataHotest($page,$num_per_page,$router='user/index',$type_online=2){
        $cacheService = new CacheService("Game","getDataHotest",$page.$num_per_page.$router);
        $key = $cacheService->createKey();
        $cache = Yii::app()->cache->get($key);
        $cache=false;
        if($cache==false){
            $connect =Yii::app()->db;
            if($type_online >= 2){   
                $sub_sql='status=1 AND isHot =1';
            }else{
                $sub_sql='status=1 AND isHot =1 AND isOnline='.$type_online;
            }
            $sql="SELECT count(id) as total FROM c_game WHERE ".$sub_sql."";
            $command=$connect->createCommand($sql);
            $row = $command->queryRow();
            $total=$row['total'];
            $begin = ($page - 1) * $num_per_page;
            $sql="SELECT id,categoryId,title,alias,introtext,picture,download,hit,create_date,isHot,percent_wapmaster,isOnline,isSms 
            FROM c_game WHERE ".$sub_sql." ORDER BY create_date DESC LIMIT ".$begin.",".$num_per_page."";
            $command=$connect->createCommand($sql);
            $rows = $command->queryAll();            

            $num_page=ceil($total/$num_per_page);
            $url=Url::createUrl($router,array('type_cat'=>$type_online,'type_data'=>3));
            $url1=$url;
            $url.='/';
            $paging=Paging::show_paging_wap_user($num_page,$page,$url,$url1);
            $a=array($rows,$paging);
            
            Yii::app()->cache->set($key, $a, ConstantsUtil::TIME_CACHE_900);
        }else{
            $a = $cache;
        }
        return $a;
    }
    public function getDataByType($isOnline,$page,$num_per_page,$router='user/index'){
        $cacheService = new CacheService("Game","getDataLastest",$page.$num_per_page.$router);
        $key = $cacheService->createKey();
        $cache = Yii::app()->cache->get($key);
        $cache=false;
        if($cache==false){
            $connect =Yii::app()->db;  
            if($isOnline==1){
                $sub_sql='status=1 AND isOnline = 1';
            }else{
                $sub_sql='status=1 AND isOnline = 0';
            }
            
            $sql="SELECT count(id) as total FROM c_game WHERE ".$sub_sql."";
            $command=$connect->createCommand($sql);
            $row = $command->queryRow();
            $total=$row['total'];
            $begin = ($page - 1) * $num_per_page;
            $sql="SELECT id,categoryId,title,alias,introtext,picture,download,hit,create_date,isHot,percent_wapmaster,isOnline,isSms 
            FROM c_game WHERE ".$sub_sql." ORDER BY create_date DESC LIMIT ".$begin.",".$num_per_page."";
            $command=$connect->createCommand($sql);
            $rows = $command->queryAll();            

            $num_page=ceil($total/$num_per_page);
            $url=Url::createUrl($router,array('type_cat'=>$isOnline));
            $url1=$url;
            $url.='/';
            $paging=Paging::show_paging_wap_user($num_page,$page,$url,$url1);
            $a=array($rows,$paging);
            
            Yii::app()->cache->set($key, $a, ConstantsUtil::TIME_CACHE_900);
        }else{
            $a = $cache;
        }
        return $a;
    }
    
    public function getGameById($game_id)
    {
        $cacheService = new CacheService("Game","getGameById",$game_id);
        $key = $cacheService->createKey();
        $cache = Yii::app()->cache->get($key);
        $cache=false;
        if ($cache == false) 
        {
            $connect =Yii::app()->db;
            $sql="SELECT * FROM c_game WHERE id=".$game_id." AND status=1";
            $command=$connect->createCommand($sql);
            $row = $command->queryRow();
            Yii::app()->cache->set($key, $row, ConstantsUtil::TIME_CACHE_86400);
        }
        else
        {
            $row=$cache;
        }
        return $row;
    }
}
?>
